package com.crack.nga2.volley;

import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.crack.nga2.entity.ForumInfo;
import com.crack.nga2.entity.SubForum;
import com.crack.nga2.entity.Theme;
import com.crack.nga2.entity.data.ParsedThemeData;
import com.crack.nga2.entity.data.ThemeData;
import com.crack.nga2.utils.DataUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.Map;

/**
 * 主题列表请求
 * Created by Administrator on 2016/1/5.
 */
public class ThemeRequest extends BaseRequest<ParsedThemeData> {
    private Response.Listener<ParsedThemeData> listener;

    public ThemeRequest(int method, String url, Response.ErrorListener errorListener, Response.Listener<ParsedThemeData> listener) {
        super(method, url, errorListener);
        this.listener = listener;

    }


    @Override
    protected Response<ParsedThemeData> parseNetworkResponse(NetworkResponse response) {
        try {
            String json = new String(response.data, "GBK");
            Gson gson = new Gson();
            String data = DataUtil.getData(json);
//                Log.i("forumAc",string);
            Type type = new TypeToken<ThemeData>() {
            }.getType();
            ThemeData themeData = gson.fromJson(data, type);
            ParsedThemeData parsedThemeData = new ParsedThemeData();
            //获取主题列表
            Map<Integer, Theme> __T = themeData.get__T();
            for (Map.Entry<Integer, Theme> entry : __T.entrySet()) {
                Theme theme = entry.getValue();
                //给标题添加颜色、样式
                DataUtil.pauseSubject(theme);
                parsedThemeData.themeList.add(theme);
            }
            //获取子版块列表
            ForumInfo forumInfo = themeData.get__F();
            parsedThemeData.topicTid = forumInfo.topped_topic;
            if (forumInfo.sub_forums != null) {
                for (Map.Entry<Integer, Map<Integer, String>> entry : forumInfo.sub_forums.entrySet()) {
                    Map<Integer, String> value = entry.getValue();
                    SubForum forum = new SubForum();
                    forum.setFid((int) Double.parseDouble(value.get(0)));
                    forum.setName(value.get(1));
                    forum.setInfo(value.get(2));
                    //判断是否被用户选择显示
                    if (forumInfo.__SELECTED_FORUM.contains(String.valueOf(forum.getFid()))) {
                        forum.isSelected = true;
                    }
                    parsedThemeData.subForumList.add(forum);
                }
            }
            return Response.success(parsedThemeData, HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return Response.error(new VolleyError(e.getMessage()));
        }
    }

    @Override
    protected void deliverResponse(ParsedThemeData response) {
        if (listener != null) {
            listener.onResponse(response);
        }
    }
}
